initConfig();
var num_ = 1;
function initConfig(){
    // 一直保持屏幕常亮
    device.keepScreenOn();
    //显示控制台
    var middle = device.width / 2 - 400;
    console.setSize(800, 800);
    console.setPosition(middle, 0);
    console.show();
    console.info("请手动打开需要滑动的软件...");
    sleep(2000);

    setInterval(function(){
        huadong();
        test();
    },1000);
}

function test(){

    var height = parseInt(device.height);
    var width = parseInt(device.width);

    var r = random(4000,8000);
    //400/4000  1/10
    if(r<6000){
        console.log("执行 【随机点赞】 事件...");
        //经软件判断在屏幕左下2/3处执行双击点赞事件最合理，不会有
        var dz_x1 = 100 + random(80,180);
        var dz_y1 = height*2/3 + random(80,180);
        click(dz_x1,dz_y1);
        click(dz_x1,dz_y1);
        click(dz_x1,dz_y1);
    }

    if(className("android.widget.Button").text("继续看视频").exists()){
        className("android.widget.Button").text("继续看视频").findOnce().click();
        console.log("执行 【继续看视频】 事件...");
    }
    if(className("android.widget.TextView").text("点击进入直播间").exists()){
        console.log("当前视频是直播，直接跳过...");
        swipe(random(80,180),height-200 ,random(80,180),150,200);
    }
    if(className("android.widget.TextView").text("点击打开图集").exists()){
        console.log("当前视频是图片，直接跳过...");
        swipe(random(80,180),height-200 ,random(80,180),150,200);
    }
    if(className("android.widget.TextView").text("点击打开长图").exists()){
        console.log("当前视频是图片，直接跳过...");
        swipe(random(80,180),height-200 ,random(80,180),150,200);
    }

    if(className("android.view.View").text("立即签到").exists()){
        className("android.view.View").text("立即签到").findOnce().click();
        console.log("执行 【立即签到】 事件...");
    }
    
    if(id("btn_confirm").exists()){
        console.info("存在直播按钮点击桌面");
        //刷宝短视频，直播任务
        var dz_x1 = 100 + random(80,180);
        var dz_y1 = height*2/3 + random(80,180);
        click(dz_x1,dz_y1);
    }

    if(id("btn_close").exists()){
        console.info("存在直播活动按钮1");
        id("btn_close").click();
    }

    if(id("iv_close").exists()){
        console.info("存在直播活动按钮2");
        id("iv_close").click();
    }
}

/**i+
 * 
 * @param {*滑动时间} duration 
 */
function huadong(){
    //随机等待时间random（最小值，最大值）根据需要更改数值（单位秒）
    var time_random=random(4000,8000);
    var height = parseInt(device.height);
    var width = parseInt(device.width);

    var sjs = random(1,100);
    var t = "u";
    // if(sjs>30){//随机上滑
        t = "u";
        var x2 = random(50,200);
        var y2 = random(80,180);
        randomSwipe(width-x2,height-y2 ,width-x2,y2);
    // }else{//随机下滑
    //     t = "d";
    //     var x2 = random(50,120);
    //     swipe(x2,150, x2,height-200 ,320);
    // }
    console.info("总浏览次数"+t+": "+num_+"次 ~ "+time_random);
    num_++;
    sleep(time_random);
}


/**
 * 真人模拟滑动函数
 *
 * 传入值：起点终点坐标
 * 效果：模拟真人滑动
 */
function randomSwipe(sx, sy, ex, ey) {
    //设置控制点极限距离
    var leaveHeightLength = 300

    //根据偏差距离，应用不同的随机方式
    var my = (sy + ey) / 2
    var y2 = my + random(0, leaveHeightLength)
    var y3 = my - random(0, leaveHeightLength)

    var lx = (sx - ex) / 3
    if (lx < 0) { lx = -lx }
    var x2 = sx + lx / 2 + random(0, lx)
    var x3 = sx + lx + lx / 2 + random(0, lx)

    //获取运行轨迹，及参数
    var time = [0, random(200, 450)]
    var track = bezierCreate(sx, sy, x2, y2, x3, y3, ex, ey)
    // console.log("------"+track);
    gestures(time.concat(track))
}



function bezierCreate(x1, y1, x2, y2, x3, y3, x4, y4) {
    //构建参数
    var h = 100;
    var cp = [{ x: x1, y: y1 + h }, { x: x2, y: y2 + h }, { x: x3, y: y3 + h }, { x: x4, y: y4 + h }];
    var numberOfPoints = 100;
    var curve = [];
    var dt = 1.0 / (numberOfPoints - 1);

    //计算轨迹
    for (var i = 0; i < numberOfPoints; i++) {
        var ax, bx, cx;
        var ay, by, cy;
        var tSquared, tCubed;
        var result_x, result_y;

        cx = 3.0 * (cp[1].x - cp[0].x);
        bx = 3.0 * (cp[2].x - cp[1].x) - cx;
        ax = cp[3].x - cp[0].x - cx - bx;
        cy = 3.0 * (cp[1].y - cp[0].y);
        by = 3.0 * (cp[2].y - cp[1].y) - cy;
        ay = cp[3].y - cp[0].y - cy - by;

        var t = dt * i
        tSquared = t * t;
        tCubed = tSquared * t;
        result_x = (ax * tCubed) + (bx * tSquared) + (cx * t) + cp[0].x;
        result_y = (ay * tCubed) + (by * tSquared) + (cy * t) + cp[0].y;
        curve[i] = {
            x: result_x,
            y: result_y
        };
    }

    //轨迹转路数组
    var array = [];
    for (var i = 0; i < curve.length; i++) {
        try {
            var j = (i < 100) ? i : (199 - i);
            xx = parseInt(curve[j].x)
            yy = parseInt(Math.abs(100 - curve[j].y))
        } catch (e) {
            break
        }
        array.push([xx, yy])
    }

    return array
}